﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.SatinAlma.YeniYapi.Confirmation
{
    public partial class KonfOzet : XForm
    {
        public KonfOzet()
        {
            InitializeComponent();
        }

        private void KonfOzet_Load(object sender, EventArgs e)
        {
            cmbAksiyon.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            cmbAksiyon.Closed += cmbAksiyon_Closed;

            //((RepositoryItemGridLookUpEdit)gleUretici.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            //((RepositoryItemGridLookUpEdit)gleUretici.Edit).Closed += Konf_Closed;

            gridView1.SetViewCaption("ARTİKELLER");
            gridView2.SetViewCaption("Seçili artikelin AYRIMLARI");
            gridView3.SetViewCaption("Seçili ayrımın KONFİRMASYONLARI");
            gridView4.SetViewCaption("Seçili konfirmasyonun YÜKLEMELERİ");

            CreateVerticalGrid();
            splitContainerControl1.AutoSize();
            splitContainerControl2.AutoSize();
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView2.FocusedRowChanged += gridView2_FocusedRowChanged;
            gridView3.FocusedRowChanged += gridView2_FocusedRowChanged;


            GridMenu gm3 = GridManager.GetManager(gridView3).GridMenu;
            gm3.AddMenu("Konf. Satırı Sil", SatirSil);

            //cmbFark.EditValueChanged += cmbFark_EditValueChanged;
            //cmbFark.EditValue = "N/A";

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnGetir.Click += new EventHandler(btnGetir_Click);
            btnTemizle.Click += new EventHandler(btnTemizle_Click);


            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gridView2.RowCellStyle += gridView1_RowCellStyle;
            gridView3.RowCellStyle += gridView1_RowCellStyle;

            gm = GridManager.GetManager(gridView1);

            gridView3.RowCellClick += gridView3_RowCellClick;
        }

        private void SatirSil(object sender, EventArgs e)
        {
            DataRow dr = gridView3.GetFocusedDataRow();
            if (dr["YuklenenMt"].ToInt() == 0)
            {
                int konfId = dr["KonfId"].ToInt();
                if (XtraMessageBox.Show(konfId + " no'lu konfirmasyon satırı silinecek. Emin misiniz?", "Uyarı", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("KonfSil", konfId);
                    btnGetir.PerformClick();
                }
            }
            else
                ShowErrorMessage("Üzerinde yükleme tanımlı olduğu için silemezsiniz! ");
        }

        void gridView3_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "RevCnt")
                {
                    int RevCnt = gridView3.GetFocusedRowCellValue("RevCnt").ToInt();
                    if (RevCnt > 0)
                    {
                        int KonfId = gridView3.GetFocusedRowCellValue("KonfId").ToInt();
                        using (RevShipDate rsd = new RevShipDate(KonfId))
                            rsd.ShowDialog();
                    }
                }
        }

        void cmbAksiyon_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            //Yenile();
        }

        private void AutoKonf(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();

            string sr = gridView1.GetSelectedValues("ProductId");

            foreach (DataRow r in gridView2.DataTable().Select("ProductId in (" + sr + ",0)"))
                if (r["KalanKonf"].ToInt() > 0)
                    r["YeniMt"] = r["KalanKonf"];

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        private void AutoYukl(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            gridView3.EndEditing();

            string sr = gridView1.GetSelectedValues("ProductId");

            foreach (DataRow r in gridView2.DataTable().Select("ProductId in (" + sr + ",0)"))
                foreach (DataRow r2 in gridView3.DataTable().Select("AyrimlarId in (" + r["AyrimlarId"] + ",0)"))
                    if (r["KalanYukleme"].ToInt() > 0)
                        r["YeniMt"] = r["KalanYukleme"];

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }


        private void InitGrids()
        {
            this.Text = cmbAksiyon.Text;

            if (intParse(GetVal("POId")) > 0 ||
                intParse(GetVal("POLinesId")) > 0 ||
                intParse(GetVal("AyrimlarId")) > 0
                )
                vgc.SetCellValue(vgc.Rows["VendorId"], 0, null);

            if (intParse(GetVal("VendorId")) == 0)
                KonfCheck();

            if (intParse(GetVal("VendorId")) > 0)
            {
                gridView1.Init("GetKonfOzet",
                    strParse(GetVal("Durum")),
                    Aksiyon,
                    intParse(GetVal("VendorId")),
                    intParse(GetVal("POId")),
                    intParse(GetVal("POLinesId")),
                    intParse(GetVal("AyrimlarId")),
                    intParse(GetVal("KonfId")),
                    intParse(GetVal("YuklemeId")),
                    intParse(GetVal("BrandId")),
                    intParse(GetVal("SezonId")),
                    strParse(GetVal("OrderRef")),
                    strParse(GetVal("MusteriKodu")),
                    strParse(GetVal("UreticiKodu")),
                    strParse(GetVal("KonfirmasyonNo")),
                    intParse(GetVal("TeslimSekliId")),
                    intParse(GetVal("ShipMethodId")),
                    datetimeParse(GetVal("ShipDate")),
                    intParse(GetVal("CarrierId")),
                    strParse(GetVal("IrsaliyeNo"))
                     );

                //gvCELines.FocusedRowHandle = gvCELines.LocateByDisplayText(0, gvCELines.Columns["Hit"], "1");
                //gvCELines2.FocusedRowHandle = gvCELines2.LocateByDisplayText(0, gvCELines2.Columns["Hit"], "1");

                gridView1.AddDetailGrid(gridView2, "ProductId", 1);
                gridView2.AddDetailGrid(gridView3, "AyrimlarId", 2);
                gridView3.AddDetailGrid(gridView4, "KonfId", 3);

                SutunYenile();

                gridView2.ShowingEditor += gridView2_ShowingEditor;

                gridView1.BestFitColumns();
                gridView2.BestFitColumns();
                gridView3.BestFitColumns();
                gridView4.BestFitColumns();

                Notify("Done.");
            }
        }

        void gridView2_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            bool ret = false;
            if (Aksiyon == 4)
                if (gridView2.GetFocusedDataRow()["KonfMt"].ToDecimal() > 0)
                    ret = true;

            e.Cancel = ret;
        }


        void KonfCheck()
        {
            DataTable t = ExecuteDataTable("GetKonfOzet",
                strParse(GetVal("Durum")),
                Aksiyon,
                0,
                intParse(GetVal("POId")),
                intParse(GetVal("POLinesId")),
                intParse(GetVal("AyrimlarId")),
                intParse(GetVal("KonfId")),
                intParse(GetVal("YuklemeId")),
                intParse(GetVal("BrandId")),
                intParse(GetVal("SezonId")),
                strParse(GetVal("OrderRef")),
                strParse(GetVal("MusteriKodu")),
                strParse(GetVal("UreticiKodu")),
                strParse(GetVal("KonfirmasyonNo")),
                intParse(GetVal("TeslimSekliId")),
                intParse(GetVal("ShipMethodId")),
                datetimeParse(GetVal("ShipDate")),
                intParse(GetVal("CarrierId")),
                strParse(GetVal("IrsaliyeNo"))
                 );

            if (t.Rows.Count > 1)
            {
                using (UreticiSec s = new UreticiSec(t))
                    if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        vgc.SetCellValue(vgc.Rows["VendorId"], 0, s.VendorId);
            }
            else if (t.Rows.Count > 0)
            {
                if (t.Rows[0]["VendorId"].ToInt() > 0)
                    vgc.SetCellValue(vgc.Rows["VendorId"], 0, t.Rows[0]["VendorId"].ToInt());

            }
        }

        /*
        void cmbFark_EditValueChanged(object sender, EventArgs e)
        {
            switch (cmbFark.EditValue.ToStr())
            {
                case "Sip-Konf":
                    gridView1.ChangeExpression("FARK", "ISNULL(SipMt,0)-ISNULL(KonfMt,0)");
                    gridView1.ChangeExpression("FARKYuzde", "Iif(SipMt>0, (SipMt-ISNULL(KonfMt,0))*100/SipMt, null)");
                    break;

                case "Sip-Yuklenen":
                    gridView1.ChangeExpression("FARK", "ISNULL(SipMt,0)-ISNULL(YuklenenMt,0)");
                    gridView1.ChangeExpression("FARKYuzde", "Iif(SipMt>0, (SipMt-ISNULL(YuklenenMt,0))*100/SipMt, null)");
                    break;

                case "Sip-Gelen":
                    gridView1.ChangeExpression("FARK", "ISNULL(SipMt,0)-ISNULL(GelenMt,0)");
                    gridView1.ChangeExpression("FARKYuzde", "Iif(SipMt>0, (SipMt-ISNULL(GelenMt,0))*100/SipMt, null)");
                    break;

                case "Yuklenen-Gelen":
                    gridView1.ChangeExpression("FARK", "ISNULL(YuklenenMt,0)-ISNULL(GelenMt,0)");
                    gridView1.ChangeExpression("FARKYuzde", "Iif(YuklenenMt>0, (YuklenenMt-ISNULL(GelenMt,0))*100/YuklenenMt, null)");
                    break;

                case "Konf-Gelen":
                    gridView1.ChangeExpression("FARK", "ISNULL(KonfMt,0)-ISNULL(GelenMt,0)");
                    gridView1.ChangeExpression("FARKYuzde", "Iif(KonfMt>0, (KonfMt-ISNULL(GelenMt,0))*100/KonfMt, null)");
                    break;

                default:
                    gridView1.ChangeExpression("FARK", "");
                    gridView1.ChangeExpression("FARKYuzde", "");
                    break;
            }

            gridView1.Columns["FARK"].VisibleIndex = cmbFark.EditValue.ToStr() != "N/A" ? 200 : -1;
            gridView1.Columns["FARKYuzde"].VisibleIndex = cmbFark.EditValue.ToStr() != "N/A" ? 201 : -1;
        }
        */

        GridManager gm;
        void SutunYenile()
        {
            gm.GridMenu.RemoveMenu("Seçili satırların eksik KONF. metrajlarını tamamla");
            gm.GridMenu.RemoveMenu("Seçili satırların eksik YÜKLEME metrajlarını tamamla");

            gridView1.FormatAsNumber(1, "SipMt", "KonfMt", "YuklenenMt", "GelenMt");
            gridView1.AddSummary("SipMt", "KonfMt", "YuklenenMt", "GelenMt");

            gridView2.FormatAsNumber(1, "SipMt", "KonfMt", "YuklenenMt", "GelenMt");
            gridView2.AddSummary("SipMt", "KonfMt", "YuklenenMt", "GelenMt");

            gridView3.FormatAsNumber(1, "KonfMt", "YuklenenMt", "GelenMt");
            gridView3.AddSummary("KonfMt", "YuklenenMt", "GelenMt");

            gridView4.FormatAsNumber(1, "YuklenenMt", "GelenMt");
            gridView4.AddSummary("YuklenenMt", "GelenMt");

            gridView2.MakeColumnInvisible("Scan", "AyrimlarId");
            gridView3.MakeColumnInvisible("AyrimlarId", "EklenmeTarihi", "UserId", "KonfId");
            gridView4.MakeColumnInvisible("AyrimlarId");

            gridView4.YanYanaGetirSoluna("Fiyat", "YuklenenMt", "GelenMt");

            gridView1.MakeReadOnly();
            switch (Aksiyon)
            {
                case 1:
                    gridView2.MakeReadOnly();
                    gridView3.MakeReadOnly("KonfirmasyonNo", "KonfirmasyonTarihi", "CurrencyId", "PaymentTermId", "PaymentMethodId", "TeslimSekliId", "KonfMt", "Fiyat", "Degisim", "MasrafKalemiId", "OngShipMethodId", "RevShipMethodId", "OngShipDate", "RevShipDate", "CarrierId");
                    gridView4.MakeEditable();

                    gridView2.YanYanaGetirSoluna("GomlekTermini", "SipMt", "KonfMt", "YuklenenMt", "GelenMt");
                    gridView3.YanYanaGetirSoluna("CurrencyId", "KonfMt", "YuklenenMt", "GelenMt");

                    break;

                case 2:
                    gridView3.AddColumn("YeniMt", typeof(decimal));
                    gridView2.AddColumn("YeniMt", typeof(decimal), "SUM(Child({FK}).YeniMt)");
                    gridView1.AddColumn("YeniMt", typeof(decimal), "SUM(Child({FK}).YeniMt)");

                    gridView3.SetCaptions("YeniMt:Böl Mt");
                    gridView3.FormatAsNumber(1, "YeniMt", "Fiyat");
                    gridView3.AddSummary("YeniMt");

                    gridView2.SetCaptions("YeniMt:Böl Mt");
                    gridView2.FormatAsNumber(1, "YeniMt");
                    gridView2.AddSummary("YeniMt");

                    gridView1.SetCaptions("YeniMt:Böl Mt");
                    gridView1.FormatAsNumber(1, "YeniMt");
                    gridView1.AddSummary("YeniMt");

                    gridView2.YanYanaGetirSoluna("GomlekTermini", "SipMt", "KonfMt", "YuklenenMt", "GelenMt", "YeniMt");
                    gridView3.YanYanaGetirSoluna("CurrencyId", "KonfMt", "YuklenenMt", "GelenMt", "YeniMt");

                    gridView2.MakeReadOnly();
                    gridView3.MakeReadOnly("YeniMt");
                    gridView4.MakeReadOnly();
                    break;

                case 3:
                    gm.GridMenu.AddMenu("Seçili satırların eksik KONF. metrajlarını tamamla", AutoKonf);

                    gridView2.AddColumn("YeniMt", typeof(decimal));
                    gridView1.AddColumn("YeniMt", typeof(decimal), "SUM(Child({FK}).YeniMt)");

                    gridView2.FormatAsNumber(1, "YeniMt", "KalanKonf");
                    gridView2.AddSummary("YeniMt", "KalanKonf");

                    gridView1.FormatAsNumber(1, "YeniMt", "KalanKonf");
                    gridView1.AddSummary("YeniMt", "KalanKonf");

                    gridView2.YanYanaGetirSoluna("GomlekTermini", "SipMt", "KonfMt", "YuklenenMt", "GelenMt", "YeniMt");
                    gridView3.YanYanaGetirSoluna("CurrencyId", "KonfMt", "YuklenenMt", "GelenMt");

                    gridView2.MakeReadOnly("YeniMt");
                    gridView3.MakeReadOnly();
                    gridView4.MakeReadOnly();
                    break;

                case 4:
                    gm.GridMenu.AddMenu("Seçili satırların eksik YÜKLEME metrajlarını tamamla", AutoYukl);

                    gridView3.AddColumn("YeniMt", typeof(decimal));
                    gridView2.AddColumn("YeniMt", typeof(decimal), "SUM(Child({FK}).YeniMt)");
                    gridView2.AddColumn("YeniMtA", typeof(decimal));
                    gridView2.AddColumn("Yeni", typeof(decimal), "ISNULL(YeniMt,YeniMtA)");
                    gridView2.MakeColumnInvisible("Yeni");

                    gridView1.AddColumn("YeniMt", typeof(decimal), "SUM(Child({FK}).Yeni)");

                    gridView3.FormatAsNumber(1, "YeniMt", "Fiyat", "KalanYukleme");
                    gridView3.AddSummary("YeniMt", "KalanYukleme");

                    gridView2.FormatAsNumber(1, "YeniMt", "YeniMtA", "KalanYukleme");
                    gridView2.AddSummary("YeniMt", "YeniMtA", "KalanYukleme");

                    gridView1.FormatAsNumber(1, "YeniMt", "KalanYukleme");
                    gridView1.AddSummary("YeniMt", "KalanYukleme");

                    gridView2.YanYanaGetirSoluna("GomlekTermini", "SipMt", "KonfMt", "YuklenenMt", "GelenMt", "KalanYukleme", "YeniMt", "YeniMtA");
                    gridView3.YanYanaGetirSoluna("CurrencyId", "KonfMt", "YuklenenMt", "GelenMt", "KalanYukleme", "YeniMt");

                    gridView2.MakeReadOnly("YeniMtA");
                    gridView3.MakeReadOnly("YeniMt");
                    gridView4.MakeReadOnly();

                    break;
            }

            gridView3.YanYanaGetir("CurrencyId:Degisim");
            gridView1.YanYanaGetir("YuklenenMt:GelenMt");
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;

            if (e.RowHandle > -1)
            {
                //if (e.RowHandle == view.FocusedRowHandle)
                string Fld = e.Column.FieldName;

                if (Aksiyon == 2 && view.Name == "gridView3" && Fld == "YeniMt" ||
                    Aksiyon == 3 && view.Name == "gridView2" && Fld == "YeniMt" ||
                    Aksiyon == 4 && view.Name == "gridView2" && Fld == "YeniMtA"
                        && splitContainerControl2.PanelVisibility == SplitPanelVisibility.Panel1 ||
                    Aksiyon == 4 && view.Name == "gridView3" && Fld == "YeniMt"
                    )
                    e.Appearance.BackColor = System.Drawing.Color.YellowGreen;
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);

                switch (e.Column.FieldName)
                {
                    case "YeniMt":
                    case "YeniMtA":
                        //case "Böl":
                        //case "YFiyat":
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                        return;
                }
            }
        }

        void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            RefreshYuklemeAdedi();
        }

        bool inProgress = false;
        void RefreshYuklemeAdedi()
        {
            if (!inProgress)
            {
                inProgress = true;
                tpYukleme.PageVisible = gridView4.RowCount > 0;
                tpYukleme.Text = "Yükleme - " + gridView4.RowCount;

                if (gridView3.GridControl.DataSource != null)
                    if (gridView3.GetFocusedDataRow() != null)
                        splitContainerControl2.PanelVisibility = gridView3.GetFocusedDataRow()["KonfId"].ToInt() > 0 ? SplitPanelVisibility.Both : SplitPanelVisibility.Panel1;
                inProgress = false;
            }
        }

        private void CreateVerticalGrid()
        {
            DataTable dtx = ExecuteDataTable("GetKonfVendors");
            RepositoryItemGridLookUpEdit gle = GetCustomGLE(dtx, "Name", "VendorId");

            //RepositoryItemGridLookUpEdit gle2 = GetCustomRI("Aksiyon", true, "Edit", "Konf. Parçala", "Yeni Konf.", "Yeni Yükleme");
            //gle2.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            //gleAksiyon.Properties.Assign(gle2);
            //gleAksiyon.EditValue = 1;            

            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("Durum", typeof(string), riGenel),
                AddItem("VendorId", typeof(int), gle),
                AddItem("POId", typeof(int)),
                AddItem("POLinesId", typeof(int)),
                AddItem("AyrimlarId", typeof(int)),
                AddItem("KonfId", typeof(int)),
                AddItem("YuklemeId", typeof(int)),
                AddItem("BrandId", typeof(int), RI(Rp.Brand)),
                AddItem("SezonId", typeof(int), RI(Rp.Sezon)),
                AddItem("OrderRef", typeof(string)),
                AddItem("MusteriKodu", typeof(string)),
                AddItem("UreticiKodu", typeof(string)),
                AddItem("KonfirmasyonNo", typeof(string)),
                AddItem("TeslimSekliId", typeof(int), RI(Rp.TeslimSekliA)),
                AddItem("ShipMethodId", typeof(int), RI(Rp.ShipMethod)),
                AddItem("ShipDate", typeof(DateTime)),
                AddItem("CarrierId", typeof(int), RI(Rp.Carrier)),
                AddItem("IrsaliyeNo", typeof(string))
            });

            //vgc.Rows.GetRowByFieldName("Notes", true).Height = 60;
            //, GetCustomRI("AG", true, "A", "G", "Tümü")

            //((RepositoryItemGridLookUpEdit)vgc.Rows["Aksiyon"].Properties.RowEdit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            //            vgc.RecordWidth = splitContainerControl2.Panel1.Width - vgc.RowHeaderWidth - 3;

            vgc.FocusedRow = vgc.Rows["VendorId"];
            vgc.SetCellValue(vgc.Rows["Durum"], 0, "AÇIK");
            //vgc.FocusedRow = vgc.Rows["Durum"];


            vgc.BestFit();
        }

        public int Aksiyon
        {
            get
            {
                int val = 0;
                switch (cmbAksiyon.Text)
                {
                    case "Edit": val = 1; break;
                    case "Konf. Parçala": val = 2; break;
                    case "Yeni Konf": val = 3; break;
                    case "Yeni Yükleme": val = 4; break;
                }
                return val;
            }
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            Yenile();
        }

        private void Yenile()
        {
            vgc.CloseEditor();
            //if (GetVal("VendorId").ToInt() == 0)
            //{
            //    XtraMessageBox.Show("Üretici boş olamaz !");
            //    return;
            //}

            if (cmbAksiyon.Text == string.Empty)
            {
                XtraMessageBox.Show("İşlem boş olamaz !");
                return;
            }

            InitGrids();

            gridView2.BestFitColumns();
            RefreshYuklemeAdedi();
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);

            vgc.FocusedRow = vgc.Rows["VendorId"];
            vgc.SetCellValue(vgc.Rows["Durum"], 0, "AÇIK");
        }


        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            gridView3.EndEditing();
            gridView4.EndEditing();

            string xml = string.Empty;
            string xmlArt = gridView1.SerializeChanges();
            string xmlAyrim = gridView2.SerializeChanges();
            string xmlKonf = gridView3.SerializeChanges();

            if (Aksiyon == 1)
            {
                gridView3.Update("KonfId,KonfirmasyonNo,KonfirmasyonTarihi,EklenmeTarihi,UserId,CurrencyId,PaymentTermId,PaymentMethodId,TeslimSekliId,AyrimlarId,KonfMt,Fiyat,Degisim,MasrafKalemiId,OngShipMethodId,RevShipMethodId,OngShipDate,RevShipDate,CarrierId", "Konf");
                gridView4.Update("YuklemeLinesId,YuklemeId,YuklenenMt,Fiyat,MasrafKalemiId,Degisim,KonfId", "YuklemeLines");    //ikinci KAYDET'te hata verdi 5. tablo yok diye
                if (isProduction)
                    ExecuteNonQuery("SendMailShipDate");

                RefreshYuklemeAdedi();
            }
            else
                if (xmlKonf != string.Empty ||
                    xmlAyrim != string.Empty)
                {
                    DataTable d = ExecuteDataTable("GetKonfPopup", xmlAyrim, xmlKonf, Aksiyon);

                    using (Ozet o = new Ozet(d, Aksiyon))
                        if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            xml = o.xml;
                        else return;


                    int VendorId = intParse(GetVal("VendorId"));
                    if (xml != string.Empty)
                        switch (Aksiyon)
                        {
                            case 2:
                                using (YeniKonf yk = new YeniKonf(VendorId))
                                    if (yk.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        ExecuteNonQuery("UpdKonfKP", xml, yk.header, WhoAmI);   //dikkat! xmlKonf
                                        if (isProduction)
                                            ExecuteNonQuery("SendMailShipDate");

                                        Yenile();
                                    }
                                break;

                            case 3:
                                using (YeniKonf yk = new YeniKonf(VendorId))
                                    if (yk.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        ExecuteNonQuery("UpdKonfYK", xml, yk.header, WhoAmI);   //dikkat! xmlAyrim
                                        if (isProduction)
                                            ExecuteNonQuery("SendMailShipDate");

                                        Yenile();
                                    }
                                break;

                            case 4:
                                using (YeniYuk yy = new YeniYuk(VendorId))
                                    if (yy.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        ExecuteNonQuery("UpdKonfYY", xml, AppSession.OncekiYukleme.AlisFaturasiId, AppSession.OncekiYukleme.YuklemeTarihi, AppSession.OncekiYukleme.YuklemeSekliId, AppSession.OncekiYukleme.IrsaliyeNo, AppSession.OncekiYukleme.ETA, VendorId, WhoAmI); // xmlKonf

                                        DataTable by = ExecuteDataTable("CheckBitenYukleme", xml);
                                        if (by.Rows.Count > 0)
                                            using (CheckBitenYukleme y = new CheckBitenYukleme(by))
                                                y.ShowDialog();

                                        Yenile();
                                    }
                                break;
                        }
                }

        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
            RefreshYuklemeAdedi();
        }


    }
}